package com.carlinfo.common.util;

import com.alibaba.fastjson.JSONObject;

/**
 * 分页工具类
 * 
 * @author LENOVO-CRG
 *
 */
public class PageInfoUtil
{
	/**
	 * 总记录数
	 */
	private int totalRecord;

	/**
	 * 分页大小
	 */
	private int pageSize = 10;

	/**
	 * 上一页
	 */
	private int prePage;

	/**
	 * 下一页
	 */
	private int nextPage;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 当前页
	 */
	private int currentPage;

	/**
	 * 当前页的启示条数
	 */
	private int currentRecord;

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getCurrentPage()
	{
		/**
		 * 当前页小于1,将当前页设置为1
		 */
		if(this.currentPage<1)
		{
			this.currentPage=1;
		}
		if(this.getTotalRecord()>0 && this.currentPage>this.getTotalPage())
		{
			this.currentPage=this.getTotalPage();
		}
		if(this.getTotalRecord()<=0)
		{
			this.currentPage=1;
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	public int getPrePage()
	{
		this.prePage = this.getCurrentPage()-1;
		if(this.prePage<1)
		{
			this.prePage = 1;
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.getCurrentPage()+1;
		if(this.getTotalRecord()>=0 && this.nextPage>this.getTotalPage())
		{
			this.nextPage = this.getTotalPage();
		}
		if(this.nextPage==0 ||this.getTotalRecord()<0)
		{
			this.nextPage=1;
		}
		return this.nextPage;
	}

	public int getTotalPage()
	{
		if(this.getTotalRecord()%this.pageSize==0)
		{
			this.totalPage= this.getTotalRecord()/this.pageSize;
		}else
		{
			this.totalPage = this.getTotalRecord()/this.pageSize+1;
		}
		return this.totalPage;
	}

	public int getCurrentRecord()
	{
		this.currentRecord = (this.getCurrentPage()-1)*this.getPageSize();
		return currentRecord;
	}

	public String toJSON()
	{
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("currentPage", this.getCurrentPage() + "");
		resultJSON.put("pageSize", this.getPageSize() + "");
		resultJSON.put("totalRecord", this.getTotalRecord() + "");
		resultJSON.put("totalPage", this.getTotalPage() + "");
		resultJSON.put("prePage", this.getPrePage() + "");
		resultJSON.put("nextPage", this.getNextPage() + "");
		resultJSON.put("currentRecord", this.getCurrentRecord() + "");
		return resultJSON.toJSONString();
	}
	
	public static void main(String[] args)
	{
		PageInfoUtil infoUtil = new PageInfoUtil();
		infoUtil.setTotalRecord(-11);
		infoUtil.setCurrentPage(1);
		System.out.println(infoUtil.toJSON());
	}
}
